package com.pwser.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.stereotype.Service;

import com.pwser.entity.User;
import com.pwser.mapper.UserMapper;

@Service
@CacheConfig(/*cacheManager = "userCacheManager",*/ cacheNames = "userCache")
public class UserService {

	//这里的单引号不能少，否则会报错，被识别是一个对象
	private static final String CACHE_KEY = "'user'";
	private static final String DEMO_CACHE_NAME = "userCache";
	
	@Autowired
	private UserMapper userMapper;
	
//	@Qualifier("userCacheManager")
	@Autowired
	private RedisCacheManager userCacheManager;
	
	//查找用户数据
	//@Cacheable  在方法执行前Spring先是否有缓存数据，如果有直接返回。如果没有数据，调用方法并将方法返回值存放在缓存当中。
	@Cacheable(value = DEMO_CACHE_NAME, key = "'user_'+#userId")
	public User getUserById(int userId) {
		User user = userMapper.getUserById(userId);
		//若找不到缓存将打印出提示语句
		System.err.println("#####没有走缓存，去查询数据库了#####" + user.toString());
		return user;
	}
	
	//保存用户数据
	//@CacheEvict   将一条或者多条数据从缓存中删除
	@CacheEvict(value = DEMO_CACHE_NAME, key = CACHE_KEY)
	public int addUser(User user) {
		int addFlag = userMapper.addUser(user.getUserId(), user.getUserName());
		System.err.println("#####数据库保存成功#####" + user.getUserId());
		return addFlag;
	}
	
	//更新用户数据
	//@CachePut   无论怎样，都将方法的返回结果放到缓存当中。
	@CachePut(value = DEMO_CACHE_NAME, key = "'user_'+#user.getUserId()")
	public User editUser(User user) {
		userMapper.editUser(user.getUserId(), user.getUserName());
		System.err.println("#####数据库更新成功#####" + user.getUserId());
		return user;
	}

	//删除用户数据
	//@CacheEvict   将一条或者多条数据从缓存中删除
	@CacheEvict(value = DEMO_CACHE_NAME, key = "'user_'+#userId")
	public boolean delUser(int userId) {
		boolean delFlag = userMapper.delUser(userId);
		System.err.println("#####数据库删除成功#####" + userId);
		return delFlag;
	}
	
	//手动代码存入redis
	public User getUserByIdToRedis(int userId) {
		User user = userMapper.getUserById(userId);
		Cache cache = userCacheManager.getCache(DEMO_CACHE_NAME);
//		cache.put("user_" + user.getUserId(), user);
		List<User> users = cache.get("user_all", List.class);
		System.out.println(users);
//		cache.clear();
//		cache.evict("user_9999");
		System.err.println("#####手动保存缓存成功#####" + user.toString());
		return user;
	}
	
	//查询全部
	@Cacheable(value = DEMO_CACHE_NAME, key = "'user_all'")
	public List<User> getAllUsers() {
		List<User> users = userMapper.getAllUsers();
		System.err.println("#####没有走缓存，去查询数据库了#####" + users.size());
		return users;
	}
	
	//根据条件查询用户列表
	@Cacheable(value = DEMO_CACHE_NAME, key = "'user_>='+#user.getUserId()")
	public List<User> getUsersByCondition(User user) {
		List<User> users = userMapper.getUsersByCondition(user);
		System.err.println("#####没有走缓存，去查询数据库了#####" + users.size());
		return users;
	}

}